package com.wu.gumomallproduct.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wu.gumomallproduct.product.dao.BrandDao;
import com.wu.gumomallproduct.product.dao.CategoryDao;
import com.wu.common.entity.product.BrandEntity;
import com.wu.common.entity.product.CategoryEntity;
import com.wu.common.vo.product.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.common.utils.PageUtils;
import com.wu.common.utils.Query;

import com.wu.gumomallproduct.product.dao.CategoryBrandRelationDao;
import com.wu.common.entity.product.CategoryBrandRelationEntity;
import com.wu.gumomallproduct.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private BrandDao brandDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveCategoryBrandRelationEntity(CategoryBrandRelationEntity categoryBrandRelation) {
        CategoryEntity category = categoryDao.selectById(categoryBrandRelation.getCatelogId());
        BrandEntity brandEntity = brandDao.selectById(categoryBrandRelation.getBrandId());
        if (Objects.isNull(category) || Objects.isNull(brandEntity)){
            throw new RuntimeException("品牌找不到或分类找不到");
        }
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(category.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrandName(Long brandId, String name) {
        this.update(new LambdaUpdateWrapper<CategoryBrandRelationEntity>()
                .set(CategoryBrandRelationEntity::getBrandName, name)
                .eq(CategoryBrandRelationEntity::getBrandId, brandId));
    }

    @Override
    public void updateCategoryName(Long catId, String name) {
        this.update(new LambdaUpdateWrapper<CategoryBrandRelationEntity>()
                .set(CategoryBrandRelationEntity::getCatelogName, name)
                .eq(CategoryBrandRelationEntity::getCatelogId, catId));
    }

    @Override
    public List<BrandEntity> getBrandsBycatId(Long catId) {
        List<CategoryBrandRelationEntity> list = this.list(new LambdaQueryWrapper<CategoryBrandRelationEntity>().eq(CategoryBrandRelationEntity::getCatelogId, catId));
        List<Long> collect = list.stream().map(x -> x.getBrandId()).collect(Collectors.toList());
        List<BrandEntity> brands = brandDao.selectList(new LambdaQueryWrapper<BrandEntity>().in(BrandEntity::getBrandId, collect));
        return brands;
    }

    @Override
    public List<BrandVo> getBrandVosBycatId(Long catId) {
        List<BrandEntity> brandsBycatId = getBrandsBycatId(catId);
        List<BrandVo> collect = brandsBycatId.stream().map(x -> {
            return new BrandVo(x.getBrandId(), x.getName());
        }).collect(Collectors.toList());
        return collect;
    }

}